package BackgammonBot.GameIO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import BackgammonBot.GameLogic.BackgammonLogic;

/**
 * This class is in charge of read and write game files
 * 
 * @author Dennis Meyer, Sebastian Brodehl
 *
 */
public class IOGameFiles 
{
	public static BackgammonLogic readGameFile(String inputFilePath) 
	{
		BackgammonLogic gameLogic = new BackgammonLogic();
		
		BufferedReader reader = null;
		try 
		{
			reader = new BufferedReader(new FileReader(inputFilePath));
	        
			char initialPlayer = reader.readLine().trim().charAt(0);
			String[] dice = reader.readLine().split(",");
			int[] initialDice = new int[2];
			initialDice[0] = Byte.valueOf(dice[0]);
			initialDice[1] = Byte.valueOf(dice[1]);
			
	        String input;        
	        byte[] board = new byte[26];
			while( (input = reader.readLine()) != null ) 
			{
				String[] line = input.split(":");
				int fieldNumber = Integer.valueOf(line[0]);
				
				line = line[1].split(",");
				
				board[fieldNumber] = 0;
				if (fieldNumber != 0 && fieldNumber != 25)
				{
					for (int i = 0; i < 2; i++)
					{
						String player = line[i].substring(0,1);
						byte checkers = Byte.valueOf(line[i].substring(1));
						
						if (checkers != 0)
						{
							if (player.equals("w"))
							{
								board[fieldNumber] = (byte) -checkers;							
							}
							else 
							{
								board[fieldNumber] = checkers;
							}
						}
					}
				}
				else
				{
					for (int i = 0; i < 2; i++)
					{
						String player = line[i].substring(0,1);
						byte checkers = Byte.valueOf(line[i].substring(1));
						
						if (player.equals("w"))
						{
							switch (fieldNumber) 
							{
								case 0:
									gameLogic.setWhiteCheckersAtStart(checkers);
									break;
								case 25:
									gameLogic.setWhiteCheckersInHome(checkers);
									break;
								default:
									board[fieldNumber] = (byte) -checkers;
									break;
							}
						}
						else 
						{
							switch (fieldNumber) 
							{
								case 25:
									gameLogic.setBlackCheckersAtStart(checkers);
									break;
								case 0:
									gameLogic.setBlackCheckersInHome(checkers);
									break;
								default:
									board[fieldNumber] = checkers;
									break;
							}
							
						}
					}
				}
			}
			reader.close();
			
			gameLogic.setBoard(board);
			gameLogic.setInitialPlayer(initialPlayer);
			gameLogic.setIntitialDice(initialDice);
		} 
		catch (IOException e) {
			System.out.println("Wrong filepath. Try again.");
			e.printStackTrace();
		}
		
		return gameLogic;
	}	

	public static void writeGameFile(String s, String outputFilePath) 
	{
		File outputFile = new File(outputFilePath);	
		if (outputFile.exists())
		{
			outputFile.delete();
		}
 		try
 		{
			FileWriter writer = new FileWriter(outputFile ,false);
			writer.write(s);
			writer.close();	
		} 
 		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
